package com.koron.plan.web.patrollineplan.service.impl;


import com.koron.plan.web.patrollineplan.bean.po.PlanLinePatrolWorker;
import com.koron.plan.web.patrollineplan.mapper.PlanLinePatrolWorkerMapper;
import com.koron.plan.web.patrollineplan.service.PlanLinePatrolWorkerService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (PlanLinePatrolWorker)表服务实现类
 *
 * @author jzp
 * @since 2022-11-25 15:36:53
 */
@Service
public class PlanLinePatrolWorkerServiceImpl implements PlanLinePatrolWorkerService {

    /**
     * 通过ID查询单条数据
     *
     */
    @Override
    @TaskAnnotation("getById")
    public PlanLinePatrolWorker getById(SessionFactory factory, String id) {
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     *
     */
    @Override
    @TaskAnnotation("list")
    public List<PlanLinePatrolWorker> list(SessionFactory factory, PlanLinePatrolWorker planLinePatrolWorker) {
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.selectList(planLinePatrolWorker);
    }

    @Override
    public List<PlanLinePatrolWorker> listByPlanId(SessionFactory factory, String planId) {
        PlanLinePatrolWorker planLinePatrolWorker = new PlanLinePatrolWorker();
        planLinePatrolWorker.setPlanId(planId);
        return list(factory, planLinePatrolWorker);
    }

    /**
     * 新增数据
     *
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, PlanLinePatrolWorker planLinePatrolWorker) {
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.insertOrUpdate(planLinePatrolWorker) > 0;
    }

    @Override
    public boolean saveBatch(SessionFactory factory, List<PlanLinePatrolWorker> workerList) {
        if (CollectionUtils.isEmpty(workerList)) {
            return false;
        }
        // 清除id
        workerList.forEach(e -> e.setId(null));
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.insertOrUpdateBatch(workerList) > 0;
    }

    /**
     * 修改数据
     *
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, PlanLinePatrolWorker planLinePatrolWorker) {
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.update(planLinePatrolWorker) > 0;
    }

    /**
     * 通过主键删除数据
     *
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.hidden(id) > 0;
    }

    @Override
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        PlanLinePatrolWorkerMapper mapper = factory.getMapper(PlanLinePatrolWorkerMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean removeByPlanId(SessionFactory factory, String planId) {
        if (StringUtils.isEmpty(planId)) {
            return false;
        }
        List<PlanLinePatrolWorker> honorList = listByPlanId(factory, planId);
        return deleteBatchIds(factory, honorList.stream().map(PlanLinePatrolWorker::getId).collect(Collectors.toList()));
    }

}
